package cn.itcast.solr;

import cn.itcast.pojo.Heima56;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;

import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
public class SolrIndexQueryTest {
    @Test
    public void testQueryIndexTest() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");
        SolrQuery query = new SolrQuery("title:手机");
        //执行查询并得到响应
        QueryResponse response = solrServer.query(query);
        //从响应数据中获得结果数据
        SolrDocumentList results = response.getResults();
        for (SolrDocument result : results) {
            String id = (String) result.get("id");
            String title = (String) result.get("title");
            Long price = Long.valueOf(result.get("price").toString());
            System.out.println("id" + id + " title" + title + " price" + price);
        }
    }

    @Test
    public void testQueryRangeIndexTest() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");
        SolrQuery query = new SolrQuery("price:[200000 TO 500000]");
        //执行查询并得到响应
        QueryResponse response = solrServer.query(query);
        //从响应数据中获得结果数据
        SolrDocumentList results = response.getResults();
        for (SolrDocument result : results) {
            String id = (String) result.get("id");
            String title = (String) result.get("title");
            Long price = Long.valueOf(result.get("price").toString());
            System.out.println("id:" + id + " title:" + title + " price:" + price);
        }
    }

    @Test
    public void testQueryIndexWithBeanTest() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");
        SolrQuery query = new SolrQuery("title:手机 AND price:[300000 TO 500000]");
        //执行查询并得到响应
        QueryResponse response = solrServer.query(query);
        //从响应数据中获得结果数据
        List<Heima56> beans = response.getBeans(Heima56.class);
        for (Heima56 bean : beans) {
            String id = bean.getId();
            String title = bean.getTitle();
            Long price = bean.getPrice();
            System.out.println("id:" + id + " title:" + title + " price:" + price);
        }
    }

    @Test
    public void testSortQueryIndexTest() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");
        SolrQuery query = new SolrQuery("title:手机");
        query.setSort("price", SolrQuery.ORDER.desc);
        //执行查询并得到响应
        QueryResponse response = solrServer.query(query);
        //从响应数据中获得结果数据
        List<Heima56> beans = response.getBeans(Heima56.class);
        for (Heima56 bean : beans) {
            String id = bean.getId();
            String title = bean.getTitle();
            Long price = bean.getPrice();
            System.out.println("id:" + id + " title:" + title + " price:" + price);
        }
    }

    @Test
    public void testSortPageQueryIndexTest() throws SolrServerException {
        int curPage = 3;
        final int PAGE_SIZE = 5;
        int start = (curPage - 1) * PAGE_SIZE;
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");
        SolrQuery query = new SolrQuery("title:手机");
        query.setStart(start);
        query.setRows(PAGE_SIZE);
        //执行查询并得到响应
        QueryResponse response = solrServer.query(query);
        //从响应数据中获得结果数据
        List<Heima56> beans = response.getBeans(Heima56.class);
        for (Heima56 bean : beans) {
            String id = bean.getId();
            String title = bean.getTitle();
            Long price = bean.getPrice();
            System.out.println("id:" + id + " title:" + title + " price:" + price);
        }
    }

    @Test
    public void testlikeQueryIndexTest() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");
        SolrQuery query = new SolrQuery("title:viva~1");

        //执行查询并得到响应
        QueryResponse response = solrServer.query(query);
        //从响应数据中获得结果数据
        List<Heima56> beans = response.getBeans(Heima56.class);
        for (Heima56 bean : beans) {
            String id = bean.getId();
            String title = bean.getTitle();
            Long price = bean.getPrice();
            System.out.println("id:" + id + " title:" + title + " price:" + price);
        }
    }

    @Test
    public void testHighLightQueryIndexTest() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");
        SolrQuery query = new SolrQuery("title:小米手机");
        query.addHighlightField("title");
        query.setHighlightSimplePre("<em>");
        query.setHighlightSimplePost("</em>");
        //执行查询并得到响应
        QueryResponse response = solrServer.query(query);
        //从响应数据中获得结果数据
        Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
        List<Heima56> beans = response.getBeans(Heima56.class);
        for (Heima56 bean : beans) {
            String id = bean.getId();
            String title = highlighting.get(id).get("title").get(0);
            System.out.println(title);
        }
    }
}
